﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Xml;
using DPortAck.ObjectModel;

namespace DPortAck.CommandLine
{
    public class Program
    {
        static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
                throw new ArgumentException("Tests file its required!");

            string filePath = Path.GetFullPath(args[0]);

            if (!File.Exists(filePath))
                throw new ArgumentException(String.Format("File \"{0}\" does not exist!", filePath));

            List<ConnectionTest> tests = new List<ConnectionTest>();

            using (XmlReader xmlR = XmlReader.Create(filePath))
            {
                if (!xmlR.EOF && xmlR.ReadToDescendant("Tests"))
                {
                    if (xmlR.ReadToDescendant("Test"))
                    {
                        do
                        {
                            ConnectionTest test = new ConnectionTest();
                            test.Name = xmlR.GetAttribute("name");

                            string timeout = xmlR.GetAttribute("timeout");

                            if (!String.IsNullOrEmpty(timeout))
                                test.Timeout = Convert.ToInt32(timeout);

                            if (xmlR.ReadToDescendant("Machine"))
                            {
                                do
                                {
                                    MachineConnectionTest machine = new MachineConnectionTest();
                                    machine.Host = xmlR.GetAttribute("host");

                                    if (xmlR.ReadToDescendant("Port"))
                                    {
                                        do
                                        {
                                            MachinePortConnectionTest port = new MachinePortConnectionTest();

                                            port.Port = Convert.ToInt32(xmlR.GetAttribute("number"));

                                            string protocol = xmlR.GetAttribute("protocol");

                                            if (!String.IsNullOrEmpty(protocol))
                                            {
                                                port.ProtocolType = (ProtocolType)Enum.Parse(typeof(ProtocolType), protocol);
                                            }

                                            string socketType = xmlR.GetAttribute("socketType");

                                            if (!String.IsNullOrEmpty(socketType))
                                            {
                                                port.SocketType = (SocketType)Enum.Parse(typeof(SocketType), socketType);
                                            }

                                            machine.AddPortTest(port);
                                        }
                                        while (xmlR.ReadToNextSibling("Port"));
                                    }

                                    test.AddMachineConnectionTest(machine);

                                    // </Ports>
                                    xmlR.Read();

                                    // </Machine>
                                    xmlR.Read();
                                }
                                while (xmlR.ReadToNextSibling("Machine"));
                            }

                            // </Machines>
                            xmlR.Read();

                            // </Test>
                            xmlR.Read();

                            tests.Add(test);
                        }
                        while (xmlR.ReadToNextSibling("Test"));
                    }
                }
            }

            foreach (ConnectionTest test in tests)
            {
                ConnectionTestResult result = test.Execute();

                if (!result.Succeded)
                    Environment.ExitCode = 1;
            }

            if (Environment.ExitCode > 0)
                Console.WriteLine("Some tests failed! See log for details!");
        }
    }
}
